from analysis import save
from analysis.spider import MyRequest
from analysis.spider import common
import pandas as pd
from flask_sqlalchemy import SQLAlchemy

import



SQLALCHEMY_DATABASE_URI = "sqlite:///../db.sqlite"
SQLALCHEMY_TRACK_MODIFICATIONS = False
db = SQLAlchemy()

class BaseModelNoId(db.Model):
    # 抽象的  抽象的模型不会在数据库中产生表，产生映射
    __abstract__ = True

    def save_or_update(self):
        try:
            db.session.add(self)
            db.session.commit()
        except Exception as e:
            print(e)
            return False
        else:
            return True

    def delete(self):
        try:
            db.session.delete(self)
            db.session.commit()
        except Exception as e:
            print(e)
            return False
        else:
            return True


class BaseModel(BaseModelNoId):
    __abstract__ = True
    id = db.Column(db.Integer, autoincrement=True, primary_key=True)

book_name = db.Column(db.String(64), nullable=False)
book_price = db.Column(db.Float, default=1)



base_url = 'https://www.kuaidaili.com/free/'
headers = {
            "Host": "https://www.kuaidaili.com",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
            }

tree = MyRequest.get_tree(base_url,headers)
ips = tree.xpath('//div[@id="list"]/table/tbody/tr')
for ip in ips:
    ip_info = ip.xpath('./td/text()')




